home *** CD-ROM | disk | FTP | other *** search
-
- #if defined(UseDumpFiles)
- #include <DumpHeader.h>
- #endif
-
-
- #include <Drag.h>
- #include <Errors.h>
- #include "ListCode.h"
-
-
- enum {
- kNumOfRows = 14,
-
- kSourceListTopMargin = 20,
- kSourceListLeftMargin = 20,
- kSourceListWidth = 80,
-
- kDestListTopMargin = 20,
- kDestListLeftMargin = 130,
- kDestListWidth = 80
-
-
- };
-
-
- static ListHandle pSourceList, pDestList, pCurrentClickedList;
- ListClickLoopProcPtr pDefaultClickLoop;
-
-
-
- /*------------------------------------------------------------------*/
-
- void AddToList(OSType theType, ListHandle list)
- {
- Rect dataRect;
- Cell cell;
- short nuRow;
- short stringSize = sizeof(OSType);
-
- if (list == NULL) Debugger();
- dataRect = (*list)->dataBounds;
- nuRow = LAddRow(1, dataRect.bottom, list);
- SetPt(&cell, 0, nuRow);
- LSetCell(&theType, sizeof(OSType), cell, list);
- }
-
-
- /*------------------------------------------------------------------*/
-
- Boolean PtInDestList(Point localPt)
- {
- Rect rct;
-
- rct = (*pDestList)->rView;
-
- return PtInRect(localPt, &(*pDestList)->rView);
- }
-
-
-
- /*------------------------------------------------------------------*/
-
- void GetDestListRect(Rect *rct)
- {
- *rct = (*pDestList)->rView;
- }
-
-
- /*------------------------------------------------------------------*/
-
- void BuildDestList(WindowPtr win)
- {
- Rect boundsRect, dataRect;
- Point cellSize = {0, 0};
- short height;
-
- GetRectHeight(win, kNumOfRows, &height);
-
- SetRect(&boundsRect, kDestListLeftMargin, kDestListTopMargin,
- kDestListLeftMargin + kDestListWidth, kDestListTopMargin + height);
- SetRect(&dataRect, 0, 0, 1, 0);
-
- pDestList = LNew(&boundsRect, &dataRect, cellSize, 0, win,
- false, false, false, true);
-
-
- if (pDestList != NULL)
- LDoDraw(true, pDestList);
- else
- Debugger();
-
- (*pDestList)->selFlags = lOnlyOne;
-
- (*pDestList)->lClickLoop = (ListClickLoopUPP) MyClickLoop;
- }
-
-
- /*------------------------------------------------------------------*/
-
- void UpdateDestList()
- {
- if (pDestList == NULL) Debugger();
- UpdateFrameList(pDestList);
- }
-
-
- /*------------------------------------------------------------------*/
-
- static void HandleClickInDestList(WindowPtr win, Boolean newClick,
- Boolean doubleClick, Cell nuCell)
- {
- #pragma unused (doubleClick, win, newClick, nuCell)
- }
-
-
- /*------------------------------------------------------------------*/
-
- void AddToDestList(OSType theType)
- {
- AddToList(theType, pDestList);
- }
-
-
- /*------------------------------------------------------------------*/
-
- void AddToSourceList(OSType theType)
- {
- AddToList(theType, pSourceList);
- }
-
-
-
- /*------------------------------------------------------------------*/
-
- void BuildSourceList(WindowPtr win)
- {
- Rect boundsRect, dataRect;
- Point cellSize = {0, 0};
- short height;
-
- GetRectHeight(win, kNumOfRows, &height);
-
- SetRect(&boundsRect, kSourceListLeftMargin, kSourceListTopMargin,
- kSourceListLeftMargin + kSourceListWidth,
- kSourceListTopMargin + height);
- SetRect(&dataRect, 0, 0, 1, 0);
-
- pSourceList = LNew(&boundsRect, &dataRect, cellSize, 0, win,
- false, false, false, true);
-
-
- if (pSourceList != NULL)
- LDoDraw(true, pSourceList);
- else
- Debugger();
- (*pSourceList)->selFlags = lOnlyOne;
-
- (*pSourceList)->lClickLoop = (ListClickLoopUPP) MyClickLoop;
- }
-
-
- /*------------------------------------------------------------------*/
-
- void UpdateSourceList()
- {
- if (pSourceList == NULL) Debugger();
- UpdateFrameList(pSourceList);
- }
-
-
- /*------------------------------------------------------------------*/
-
- static void UpdateFrameList(ListHandle list)
- {
- if (list != NULL) {
- Rect listRect;
-
- listRect = (*list)->rView;
- InsetRect(&listRect, -1, -1);
- LUpdate((*list)->port->visRgn, list);
- FrameRect(&listRect);
- }
- }
-
-
-
- /*------------------------------------------------------------------*/
-
- static void HandleClickInSourceList(WindowPtr win, Boolean newClick,
- Boolean doubleClick, Cell nuCell)
- {
- #pragma unused (doubleClick, win, newClick, nuCell)
- }
-
-
- /*------------------------------------------------------------------*/
-
- Boolean MyClickLoop()
- {
- Point localPt;
- Cell selectedCell;
- ListHandle list;
-
- list = pCurrentClickedList;
- GetMouse(&localPt);
- if (GetFirstSelection(list, &selectedCell) == true) {
- if (PtInRect(localPt, &(*list)->rView) == false) {
- EventRecord dummyEvent;
- long tmpLong;
- Rect tmpRect;
-
- OSEventAvail(0, &dummyEvent);
- dummyEvent.what = mouseDown;
-
- //
- // We go through the trouble of making sure the mouse doesn't
- // appear outside of the list cell region when the drag starts.
- //
- // More purdy that way.
- //
-
- GetSelectionRect(list, &tmpRect);
- InsetRect(&tmpRect, 2, 2);
- tmpLong = PinRect(&tmpRect, localPt);
- dummyEvent.where = *(Point *) &tmpLong;
- LocalToGlobal((Point *) &dummyEvent.where);
-
- //
- // Now if we successfully drop the cell somewhere else,
- // clear the selection in the original list.
- //
-
- if (StartADrag(list, &dummyEvent) == noErr)
- LSetSelect(false, selectedCell, list);
-
- return false;
-
- }
- }
-
- return true;
- }
-
- /*------------------------------------------------------------------*/
-
- OSErr AddFlavors(DragReference drag, ItemReference item, ListHandle list)
- {
- OSType selectedType;
- short dataSize;
- Cell selectedCell;
-
- SetPt((Point *) &selectedCell, 0, 0);
- if (GetFirstSelection(list, &selectedCell) == false)
- return paramErr;
-
- dataSize = sizeof(OSType);
- LGetCell((Ptr) &selectedType, &dataSize, selectedCell, list);
-
- if (dataSize != sizeof(OSType))
- return paramErr;
-
- return AddDragItemFlavor(drag, item, myPersonalFlavorType,
- (Ptr) &selectedType, dataSize, flavorSenderOnly);
-
- }
-
-
- /*------------------------------------------------------------------*/
-
- OSErr StartADrag(ListHandle list, EventRecord *theEvent)
- {
- OSErr err;
- DragReference theDrag;
- Rect dragBounds;
- RgnHandle dragRgn;
- ItemReference theItem = 1;
-
- err = NewDrag(&theDrag);
-
- if (err == noErr) {
- err = AddFlavors(theDrag, theItem, list);
-
- if (err == noErr) {
-
- if (GetSelectionRect(list, &dragBounds) == true) {
- LocalToGlobalRect(&dragBounds);
- err = SetDragItemBounds(theDrag, theItem, &dragBounds);
-
- if (err == noErr) {
- dragRgn = NewRgn();
- RectRgn(dragRgn, &dragBounds);
- OutlineRegion(dragRgn);
- err = TrackDrag(theDrag, theEvent, dragRgn);
-
- DisposeRgn(dragRgn);
- }
- }
- }
-
- DisposeDrag(theDrag);
- }
-
- return err;
- }
-
-
- /*------------------------------------------------------------------*/
-
- Boolean GetFirstSelection(ListHandle list, Cell *cell)
- {
-
- SetPt((Point *) cell, 0, 0);
- return LGetSelect(true, cell, list);
- }
-
-
- /*------------------------------------------------------------------*/
-
- Boolean GetSelectionRect(ListHandle list, Rect *rct)
- {
- Boolean validSelection;
- Cell selectedCell, adjustedCell;
- Rect selectedCellRect, emptyRect = {0, 0, 0, 0};
-
- *rct = emptyRect;
- validSelection = GetFirstSelection(list, &selectedCell);
-
- if (validSelection) {
-
- //
- // adjustedCell is needed because the current view of the list may not
- // have cell (0, 0) at the top left corner. This takes the top and left
- // values of the visible field and subtracts them from the selected cell,
- // to give us this cell's location in the current view (honoring scroll bars).
- //
-
- adjustedCell.v = selectedCell.v - (*list)->visible.top;
- adjustedCell.h = selectedCell.h - (*list)->visible.left;
-
- selectedCellRect.top = (*list)->rView.top + (adjustedCell.v * (*list)->cellSize.v);
- selectedCellRect.bottom = selectedCellRect.top + (*list)->cellSize.v;
-
- selectedCellRect.left = (*list)->rView.left + (adjustedCell.h * (*list)->cellSize.h);
- selectedCellRect.right = selectedCellRect.left + (*list)->cellSize.h;
-
- *rct = selectedCellRect;
- }
-
- return validSelection;
- }
-
-
- /*------------------------------------------------------------------*/
-
- Boolean MouseHitSelection(ListHandle list, Point localPt)
- {
- Cell selectedCell;
- Rect selectedCellRect;
-
-
- if (GetFirstSelection(list, &selectedCell) == false)
- return false;
-
- if (PtInRect(* (Point *) &selectedCell, (Rect *) &(*list)->visible) == false)
- return false;
-
- if (GetSelectionRect(list, &selectedCellRect) == false)
- return false;
-
- return PtInRect(localPt, &selectedCellRect);
-
- }
-
-
- /*------------------------------------------------------------------*/
-
- void OutlineRegion(RgnHandle theRgn)
- {
- RgnHandle tempRgn;
-
- tempRgn = NewRgn();
- CopyRgn(theRgn, tempRgn);
- InsetRgn(tempRgn, 1, 1);
- DiffRgn(theRgn, tempRgn, theRgn);
- DisposeRgn(tempRgn);
- }
-
-
-
- /*------------------------------------------------------------------*/
-
- Boolean HandleListClick(WindowPtr win, EventRecord *event)
- {
- Rect listRect;
- Point localPt;
- Boolean handledClick;
- long numOfLists = 2, count;
- ListHandle thisList, lists[2];
-
- lists[0] = pSourceList;
- lists[1] = pDestList;
-
- handledClick = false;
- localPt = event->where;
- GlobalToLocal(&localPt);
-
- for (count = 0; (count < numOfLists) && (handledClick == false) ; count ++) {
- thisList = lists[count];
-
- if ((*thisList)->lActive == true) {
-
- listRect = (*thisList)->rView;
- if ((*thisList)->vScroll != NULL) listRect.right += 15;
- if ((*thisList)->hScroll != NULL) listRect.bottom += 15;
-
- if (PtInRect(localPt, &listRect) == true) {
- Cell oldCell, nuCell;
- Boolean oldSelection,
- validSelection = false,
- newClick = false,
- doubleClick;
-
- newClick = false;
-
- oldSelection = GetFirstSelection(thisList, &oldCell);
-
- if (MouseHitSelection(thisList, localPt)) {
-
- //
- // Within this list, we may have a cell selected that the user
- // now wants to drag. MouseHitSelection will first check if
- // the user wants to do such a thing, and if so it will start
- // the drag with the appropriate information.
- //
- // Now if we successfully drop the cell somewhere else,
- // clear the selection in the original list.
- //
-
- if (StartADrag(thisList, event) == noErr)
- LSetSelect(false, oldCell, thisList);
- return true;
- }
-
- //
- // Save the current list for use in our click loop
- //
-
- pCurrentClickedList = thisList;
-
- doubleClick = LClick(localPt, event->modifiers, thisList);
- validSelection = GetFirstSelection(thisList, &nuCell);
-
- if (validSelection == true)
- if ((oldSelection == false)
- || (EqualCells(nuCell, oldCell) == false))
-
- newClick = true;
-
- if (thisList == pSourceList)
- HandleClickInSourceList(win, newClick, doubleClick, nuCell);
- else
- HandleClickInDestList(win, newClick, doubleClick, nuCell);
-
- handledClick = true;
- }
- }
- }
- return handledClick;
- }
-
-
- /*------------------------------------------------------------------*/
-
- void GetRectHeight(WindowPtr win, short rows, short *height)
- {
- #pragma unused(win)
- FontInfo fi;
-
- GetFontInfo(&fi);
- *height = (fi.ascent + fi.descent + fi.leading) * rows;
- }
-
-
- /*------------------------------------------------------------------*/
-
- static Boolean EqualCells(Cell c1, Cell c2)
- {
- return ((c1.h == c2.h) && (c1.v == c2.v));
- }
-
-
- /*------------------------------------------------------------------*/
-
- void GetGlobalMouse(Point *mouse)
- {
- EventRecord event;
-
- OSEventAvail(0, &event);
- *mouse = event.where;
- }
-
-
- /*------------------------------------------------------------------*/
-
- void GlobalToLocalRect(Rect *r)
- {
- GlobalToLocal((Point*)(&r->top));
- GlobalToLocal((Point*)(&r->bottom));
- }
-
-
- /*------------------------------------------------------------------*/
-
- void LocalToGlobalRect(Rect *r)
- {
- LocalToGlobal((Point*)(&r->top));
- LocalToGlobal((Point*)(&r->bottom));
- }
-
-
-